package com.njwb.day18.thd04;

public class AccountRunnable implements Runnable{
    private Account account  = new Account() ;
    @Override
    public void run() {
        while(account.getMoney()>=100) {
            withDrawMoney();
        }
    }
//    public void withDrawMoney(){
    //同步代码块
//        synchronized (account) {
//            if(account.getMoney()>=100) {
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                int money = account.getMoney();
//                account.setMoney(money-100);
//                System.out.println(Thread.currentThread().getName()+"取走了100块,还剩下"+account.getMoney());
//            }
//        }
//
//    }

    // 成员方法=》  AccountRunnable的当前实例 this
    public synchronized void withDrawMoney(){
        if(account.getMoney()>=100) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int money = account.getMoney();
            account.setMoney(money-100);
            System.out.println(Thread.currentThread().getName()+"取走了100块,还剩下"+account.getMoney());
        }
    }

}
